package org.hxyjs.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hxyjs.aop.SystemMail;
import org.hxyjs.dto.DTO;
import org.hxyjs.entity.Orderrecord;
import org.hxyjs.entity.Parents;
import org.hxyjs.entity.Parents;
import org.hxyjs.page.PageInfo;
import org.hxyjs.service.ParentsService;
import org.hxyjs.vo.ParentsVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@CrossOrigin
@Controller
@RequestMapping("/parentsController")
public class ParentsController {
	@Resource
	ParentsService parentsService;
	@RequiresPermissions("parents:getAll")
	@RequestMapping("/getAllParents")
	@ResponseBody //把方法的返回值按json转换之后 输出到浏览器
    public DTO<Parents> getAllParents() throws Exception {
    	System.out.println("查询全部家长");
		return parentsService.getAllParentss();
    }

	@RequestMapping("/deleteParentsByPno/{pno}")
	@ResponseBody
    public DTO<Parents> deleteParentsByPno(@PathVariable("pno") String pno) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
    	System.out.println(pno+"删除家长");
    	return parentsService.deleteParentsByPno(pno);
    }

	@RequestMapping("/updateParentsByPno")
	@ResponseBody
	public DTO<Parents> updateParentsByPno(@RequestBody Parents parents) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("修改家长");
		return parentsService.updateParentsByPno(parents);
	}
    @SystemMail(op = "Parents:addParents",queueName = "family.mail",module = "家长管理",methods = "家长注册")
	@RequestMapping("/addParents")
	@ResponseBody
	public DTO<Parents> addParents(@RequestBody Parents parents) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("增加家长");
		System.out.println(parents);
 		return parentsService.addParents(parents);
	}

	@RequestMapping("/getParentsByPage")
	@ResponseBody
	public DTO<Parents> getParentsByPage(PageInfo pageInfo) throws Exception{//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("分页家长");
		return parentsService.getParentsByPage(pageInfo);
	}
	//@RequiresPermissions("parents:selectParentsWithChildren")
	@RequestMapping("/selectParentsWithChildren/{pno}")
	@ResponseBody
	public DTO<Parents> selectParentsWithChildren(@PathVariable("pno") String pno) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("查询家长附上儿童");
		return parentsService.selectParentsWithChildren(pno);
	}
	//@RequiresPermissions("parents:getParentsWithOrdrees")
	@RequestMapping("/getParentsWithOrdrees/{pno}")
	@ResponseBody
	public DTO<Parents> getParentsWithOrdrees(@PathVariable("pno") String pno ) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("查询订单");
		return parentsService.getParentsWithOrdrees(pno);
	}

	@RequestMapping("/getOrderCookie/{pno}")
	@ResponseBody
	public DTO<Parents> getOrderCookie(@PathVariable("pno") String pno, HttpServletRequest request, HttpServletResponse response) throws Exception{
		//cookie是以key value形式存放在浏览器的文本
		ObjectMapper objectMapper = new ObjectMapper();
		DTO dto = new DTO(200, "cookie选课获取成功");
		Cookie[] cookies = request.getCookies();
		Parents parents = null;
		if(Optional.ofNullable(cookies).isPresent()){
			List<Orderrecord> orderrecords = new ArrayList<Orderrecord>();
			for(Cookie cookie :cookies){
				if(cookie.getName().indexOf("parents" + pno+"_")>=0){
					Orderrecord orderrecord = objectMapper.readValue(URLDecoder.decode(cookie.getValue()), Orderrecord.class);
					orderrecords.add(orderrecord);
				}
				if(cookie.getName().equals("parents" + pno)){
					parents = objectMapper.readValue(URLDecoder.decode(cookie.getValue()), Parents.class);
				}

			}
			if(Optional.ofNullable(parents).isPresent()){
				parents.setOrderrecords(orderrecords);
				dto.setT(parents);
			}else{
				dto.setCode(404);
				dto.setMsg("没有cookie");
			}
		}


		return dto;
	}

//	public DTO<Parents> saveSelectCourseCookieProcess(Parents parents, HttpServletRequest request, HttpServletResponse response) throws JsonProcessingException, UnsupportedEncodingException {
//		//cookie是以key value形式存放在浏览器的文本
//		DTO dto = new DTO(200,"订单保存成功");
//		if(parents.getPno()!=null) {
//			ObjectMapper objectMapper = new ObjectMapper();
//			List<Orderrecord> orderrecords = new ArrayList<>(parents.getOrderrecords());
//			for (Orderrecord orderrecord : parents.getOrderrecords()) {
//				Cookie orderrecordsCookie = new Cookie("parents" + parents.getPno() + "_" + orderrecord.getOrrno(), URLEncoder.encode(objectMapper.writeValueAsString(orderrecord), "UTF-8"));
//
//				//parentsCookie.setMaxAge(30*24*60*60);   //存活期为一个月 30*24*60*60
//				orderrecordsCookie.setPath("/");
//				orderrecordsCookie.setDomain("localhost");
//				orderrecordsCookie.setMaxAge(2 * 24 * 60 * 60);   //存活期为2天 2*24*60*60
//				response.addCookie(orderrecordsCookie);
//
//			}
//			parents.setOrderrecords(null);
//			Cookie parentsCookie = new Cookie("parents" + parents.getPno(), URLEncoder.encode(objectMapper.writeValueAsString(parents), "UTF-8"));
//			parentsCookie.setPath("/");
//			parentsCookie.setDomain("localhost");
//			parentsCookie.setMaxAge(2 * 24 * 60 * 60);   //存活期为2天 2*24*60*60
//			response.addCookie(parentsCookie);
//			parents.setOrderrecords(orderrecords);
//		}else{
//			dto.setCode(406);
//			dto.setMsg("缺少必要的参数，非法操作");
//		}
//		return dto;
//	}

@RequestMapping("/sendCodeMail/{pname}")
@ResponseBody
public DTO<Parents> sendCodeMail(@PathVariable("pname") String pname) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
	System.out.println("找回密码");
	return parentsService.sendCodeMail(pname);
}
	@RequestMapping("/updateParentsByPwd")
	@ResponseBody
	public DTO<Parents> updateParentsByPwd( ParentsVo parentsVo) throws Exception {//如何从页面传入参数到方法 用这个注解绑定变量@PathVariable
		System.out.println("修改密码"+parentsVo);
		System.out.println(parentsVo);
		return parentsService.updateParentsByPwd(parentsVo);
	}

	@RequestMapping("/activate/{pno}")
	@ResponseBody
	public DTO<Parents> activate(@PathVariable("pno") String pno) throws Exception{

		return parentsService.activate(pno);
	}

}